package shun.demo.bitmap;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.ImageView;
import shun.demo.R;

public class BitmapActivity extends ActionBarActivity implements View.OnClickListener {
    private ImageView mImageView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bitmap);

        findViewById(R.id.button).setOnClickListener(this);
        mImageView = (ImageView) findViewById(R.id.image);

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(getResources(), R.drawable.lx1, options);
        int originWidth = options.outWidth;
        int originHeight = options.outHeight;
        options.inSampleSize = 2;
        options.inJustDecodeBounds = false;
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lx1, options);
        int width = bitmap.getWidth();
        int heith = bitmap.getHeight();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void onClick(View v) {
        Matrix matrix = null;
        if (matrix == null) {
            matrix = new Matrix();
        }
        mImageView.setScaleType(ImageView.ScaleType.MATRIX);
        int dwidth = mImageView.getDrawable().getIntrinsicWidth();
        int dheight = mImageView.getDrawable().getIntrinsicHeight();
        int vwidth = mImageView.getWidth();
        int vheight = mImageView.getHeight();
        RectF mTempSrc = new RectF();
        RectF mTempDst = new RectF();
        mTempSrc.set(0, 0, dwidth, dheight);
        mTempDst.set(0, 0, vwidth, vheight);
        matrix.setRectToRect(mTempSrc, mTempDst, Matrix.ScaleToFit.CENTER);
        matrix.preRotate(90, 48, 48);
        mImageView.setImageMatrix(matrix);

    }
}
